<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>手动实现map-forEach-filter</title>
  </head>
  <body>
    <script>
      // 手动实现map
      Array.prototype.myMap = function () {
        var arr = this;
        var [fn, thisValue] = Array.prototype.slice.call(arguments);
        var result = [];
        for (i = 0; i < arr.length; i++) {
          result.push(fn.call(thisValue, arr[i], i, arr));
        }
        return result;
      };
      var arr0 = [1, 2, 3, 4];
      console.log(arr0.myMap((v) => v + 1));



      // forEach实现(reduce类似)
      Array.prototype.myForEach = function (fn, thisValue) {
        var result = [];
        this.forEach((v, i, arr) => {
          result.push(fn.call(thisValue, v, i, arr));
        });
        return result;
      };
      var arr0 = [1, 2, 3];
      console.log(arr0.myForEach((v) => v + 1));
    </script>
  </body>
</html>
